﻿<template>
  <a-modal :title="title" width="40%" :visible="visible" :confirmLoading="loading" @ok="handleSubmit"
    @cancel="() => { this.visible = false }">
    <a-spin :spinning="loading">
      <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
        <a-form-model-item label="插件名称" prop="ClassName">
          <a-input v-model="entity.ClassName" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="任务名称" prop="JobName">
          <a-input v-model="entity.JobName" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="时间单位" prop="Unit">
          <a-select show-search placeholder="请选择" option-filter-prop="children" style="width: 100%"
            @change="unit_handleChange" v-model="entity.Unit">
            <a-select-option value="Date">
              日期
            </a-select-option>
            <a-select-option value="Day">
              天
            </a-select-option>
            <a-select-option value="Hour">
              小时
            </a-select-option>
            <a-select-option value="Minute">
              分钟
            </a-select-option>
            <a-select-option value="Second">
              秒
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item  label="执行周期" prop="TimeSpan">
          <a-input-number v-model="entity.TimeSpan" :min="1" :max="65535" style="width: 100%" :disabled="entity.Unit=='Date'||entity.Unit===undefined"/>
        </a-form-model-item>
        <a-form-model-item label="每日执行时间" prop="StartTime">
          <a-time-picker @change="onChange" format="HH:mm"  style="width: 100%" />
        </a-form-model-item>
        <a-form-model-item label="状态" prop="State">
          <a-input v-model="entity.State" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="消息" prop="Message">
          <a-input v-model="entity.Message" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="备注" prop="Notes">
          <a-input v-model="entity.Notes" autocomplete="off" />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { watch } from 'less';
import moment from 'moment';
export default {
  props: {
    parentObj: Object
  },
  data() {
    return {
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 }
      },
      visible: false,
      loading: false,
      entity: {},
      rules: {},
      title: '',
      startTime:null
    }
  },
  methods: {
    onChange(time, timeString){
      this.entity.StartTime=timeString;
    },
    init() {
      this.visible = true
      this.entity = {}
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    openForm(id, title, selectedRows) {
      this.init()
      if (id) {
        this.loading = true
        this.$http.post('/Base_Manage/Base_Job/GetTheData', { id: id }).then(resJson => {
          this.loading = false
          this.entity = resJson.Data
        })
      }
      else {
        if (selectedRows.length > 0 && title === 'Add') {
          this.entity=JSON.parse(JSON.stringify(selectedRows[0]))
          this.entity.Id = '';
        }
      }
    },
    handleSubmit() {
      console.log(this.entity);
      
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        this.loading = true
        this.$http.post('/Base_Manage/Base_Job/SaveData', this.entity).then(resJson => {
          this.loading = false

          if (resJson.Success) {
            this.$message.success('操作成功!')
            this.visible = false

            this.parentObj.getDataList()
          } else {
            this.$message.error(resJson.Msg)
          }
        })
      })
    },
    unit_handleChange(value) {
      this.entity.unit = value;
    },
  }/* ,
  watch: {
    'entity.StartTime': function (val) {
      if (val !== undefined) {
        this.startTime = moment(this.entity.StartTime).format("HH:mm");
      }
    }
  } */
}
</script>
